Title: SQL W3S (By-BKK) / 024 Aliases
Tags: Obsidian_to_Anki

Q: What is an SQL alias?
A: A temporary name given to a table or column in a SQL query

Q: Why are SQL aliases used?
A: To make column or table names more readable

Q: How long does an alias exist in SQL?
A: Only for the duration of the query

Q: Which keyword is used to create an alias in SQL?
A: AS

Q: Is the AS keyword mandatory for creating an alias in SQL?
A: False

Q: Can you create an alias without using AS in SQL?
A: Yes, you can skip AS and just write the alias after the column name

Q: Write a query to create an alias 'ID' for the column CustomerID
A: SELECT CustomerID AS ID FROM Customers;

Q: SELECT CustomerID AS ID FROM Customers;
A: This query creates an alias 'ID' for the column CustomerID

Q: How do you write a query to create an alias without AS?
A: SELECT CustomerID ID FROM Customers;

Q: What is the syntax for aliasing a column in SQL?
A: SELECT column_name AS alias_name FROM table_name;

Q: What is the syntax for aliasing a table in SQL?
A: SELECT column_name(s) FROM table_name AS alias_name;

Q: How do you create multiple aliases for columns in a SQL query?
A: Separate each alias by a comma in the SELECT statement

Q: Why might you want to use an alias with spaces, like "My Great Products"?
A: To make column names more descriptive and readable

Q: How do you create an alias with spaces in SQL?
A: Use square brackets [ ] or double quotes " " around the alias

Q: How does MySQL handle aliases with spaces?
A: Use double quotes or square brackets depending on the database settings

Q: True or False: All databases support both square brackets and double quotes for aliases with spaces
A: False

Q: How can you combine multiple columns and give them an alias in SQL?
A: Use string functions like CONCAT or +, and assign the result an alias

Q: Write a query to concatenate address columns into a single alias in MySQL
A: SELECT CustomerName, CONCAT(Address, ', ', PostalCode, ', ', City, ', ', Country) AS Address FROM Customers;

Q: Write a query to concatenate address columns into a single alias in Oracle
A: SELECT CustomerName, (Address || ', ' || PostalCode || ', ' || City || ', ' || Country) AS Address FROM Customers;

Q: How do you create an alias for a table in SQL?
A: Use the AS keyword after the table name in the FROM clause

Q: Why use table aliases in SQL queries?
A: To make SQL code shorter and simpler, especially when using multiple tables

Q: Give an example of using table aliases with two tables
A: SELECT o.OrderID, o.OrderDate, c.CustomerName FROM Customers AS c, Orders AS o WHERE c.CustomerName = 'Around the Horn' AND c.CustomerID = o.CustomerID;

Q: Which SQL statement creates an alias for the CustomerID column as 'ID'?
A: SELECT CustomerID AS ID FROM Customers;

Q: When are aliases most useful in SQL?
A: When there’s more than one table, functions are used, names are long, or columns are combined

Q: True or False: Aliases can be created for both columns and tables
A: True

Q: True or False: An alias in SQL changes the actual database column or table name
A: False

Q: A temporary name for a column or table in a SQL query
A: What is an alias in SQL?

Q: AS keyword
A: Which keyword is commonly used for creating aliases in SQL?

Q: Only for the duration of a query
A: How long does an alias last in SQL?

Q: SELECT column_name AS alias_name FROM table_name;
A: What is the syntax for aliasing a column in SQL?

Q: SELECT column_name(s) FROM table_name AS alias_name;
A: What is the syntax for aliasing a table in SQL?